<?php

namespace Gds\StatistiqueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClientController extends Controller {

    public function indexAction() {

        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        if ($request->isMethod('POST')) {
            $date1 = $request->get('date1');
            $date2 = $request->get('date2');
            $id_client = $request->get('client');
            $id_departement = $request->get('departement');
            $client = $em->getRepository('GdsVenteBundle:Client')->find($id_client);
            $departement = $em->getRepository('GdsArticleBundle:Departement')->find($id_departement);
            $list_devis = $em->getRepository('GdsVenteBundle:Devis')->getStatDevis($date1, $date2, $client);
            $statistique = array();
            $devis = array();
            $devis['cloturer'] = 0;
            $devis['non_cloturer'] = 0;
            foreach ($list_devis as $d) {
                if ($d->getIsCloturer())
                    $devis['cloturer']++;
                else
                    $devis['non_cloturer']++;
            }
            $list_bon_commande = $em->getRepository('GdsVenteBundle:BonCommandeClient')->getStatBonCommande($date1, $date2, $client, $departement);
            $bc = array();
            $bc['cloturer'] = 0;
            $bc['non_cloturer'] = 0;
            foreach ($list_bon_commande as $b) {
                if ($b->getIsCloturer())
                    $bc['cloturer']++;
                else
                    $bc['non_cloturer']++;
            }
            $list_bon_livraison = $em->getRepository('GdsVenteBundle:BonLivraisonClient')->getStatBonLivraison($date1, $date2, $client, $departement);
            $bl = array();
            $bl['regle'] = 0;
            $bl['non_regle'] = 0;
            $bl['partiellement_regle'] = 0;
            foreach ($list_bon_livraison as $b) {
                if ($b->getMontantTtc() == $b->getReste())
                    $bl['non_regle']++;
                elseif ($b->getReste() == 0)
                    $bl['regle']++;
                else
                    $bl['partiellement_regle']++;
            }
            $list_facture = $em->getRepository('GdsVenteBundle:FactureClient')->getStatFacture($date1, $date2, $client);
            $facture = array();
            $facture['regle'] = 0;
            $facture['non_regle'] = 0;
            $facture['partiellement_regle'] = 0;
            foreach ($list_facture as $f) {
                if ($f->getMontantTtc() == $f->getReste())
                    $facture['non_regle']++;
                elseif ($f->getReste() == 0)
                    $facture['regle']++;
                else
                    $facture['partiellement_regle']++;
            }
            $statistique['devis'] = $devis;
            $statistique['bon_commande'] = $bc;
            $statistique['bon_livraison'] = $bl;
            $statistique['facture'] = $facture;
            return $this->render('GdsStatistiqueBundle:Client:statistique.html.twig', array('statistique' => $statistique, 'client' => $client));
        }
        return $this->render('GdsStatistiqueBundle:Client:index.html.twig');
    }
}
